package com.ymatou.open.product.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymatou.open.product.parameter.GetProductLinkBean;
import com.ymatou.open.product.service.GetProductLinkCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.business.service.ProductManagerCallServiceV3;
import com.ymttest.business.service.ThirdpartysyncCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.utils.EnvSetup;
/**
*生成短链Test
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetProductLink{
	private static GetProductLinkBean getproductlinkBean;
	private static GetProductLinkCall getproductlinkCall;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("productsellerid"));
	private static String sellerName = EnvSetup.getData("productsellername");
	
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("生成短链Test");
	}

	@Before
	public void caseUp() {
		getproductlinkBean=new GetProductLinkBean();
		getproductlinkCall=new GetProductLinkCall();
		getproductlinkBean.setApp_key("appidabcd");
		getproductlinkBean.setSign("aa");
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getproductlinkCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductLink_001() {
		Logger.start(true,"正确数据，成功");
		try {
			AddSellerProductRequest addProdReq = ProductManagerCallServiceV3.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addProdReq).get("Data").getAsJsonObject()
					.get("ProductId").getAsString();
			Thread.sleep(500);
			//添加推广商品
			ThirdpartysyncCallService.insertPromotionProduct(productId, sellerId, 0.03);
			Thread.sleep(2000);
			ThirdpartysyncCallService.GetDouyinProductInfo(productId);
			
			getproductlinkBean.setDetail_url("http://www.ymatou.com/product/"+productId+".html");
			getproductlinkBean.setPid("pidabcd");
			getproductlinkBean.setStat_param("123");
			
			getproductlinkCall.setData(getproductlinkBean);
			getproductlinkCall.callService();			
			Logger.verifyIsContains(productId,getproductlinkCall.getString("short_url"), "验证短链接是否生成成功");
			Logger.verifyIsContains(productId,getproductlinkCall.getString("short_tma_url"), "验证short_tma_url是否生成成功");
			Logger.verifyIsContains(productId,getproductlinkCall.getString("short_app_url"), "验证short_app_url是否生成成功");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductLink_002() {
		Logger.start(true,"商品链接中id错误，失败");
		try {
			AddSellerProductRequest addProdReq = ProductManagerCallServiceV3.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addProdReq).get("Data").getAsJsonObject()
					.get("ProductId").getAsString();
			Thread.sleep(500);
			//添加推广商品
			ThirdpartysyncCallService.insertPromotionProduct(productId, sellerId, 0.03);
			Thread.sleep(2000);
			ThirdpartysyncCallService.GetDouyinProductInfo(productId);
			
			getproductlinkBean.setDetail_url("http://www.baidu.com/product/"+productId+"132.html");
			getproductlinkBean.setPid("pidabcd");
			getproductlinkBean.setStat_param("123");
			
			getproductlinkCall.setData(getproductlinkBean);
			getproductlinkCall.callService();			
			Logger.verifyIsContains("Error",getproductlinkCall.getReturnData(),"返回错误" );
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductLink_003() {
		Logger.start(true,"pid错误，失败");
		try {
			AddSellerProductRequest addProdReq = ProductManagerCallServiceV3.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addProdReq).get("Data").getAsJsonObject()
					.get("ProductId").getAsString();
			Thread.sleep(500);
			//添加推广商品
			ThirdpartysyncCallService.insertPromotionProduct(productId, sellerId, 0.03);
			Thread.sleep(2000);
			ThirdpartysyncCallService.GetDouyinProductInfo(productId);
			
			getproductlinkBean.setDetail_url("http://www.baidu.com/product/"+productId+".html");
			getproductlinkBean.setPid("pidab13cd");
			getproductlinkBean.setStat_param("123");
			
			getproductlinkCall.setData(getproductlinkBean);
			getproductlinkCall.callService();			
			Logger.verifyIsContains("Error",getproductlinkCall.getReturnData(),"返回错误" );
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}